import { api } from '@/utils/request'
import type { 
  User, 
  PageParams,
  PageResult,
  ApiResponse 
} from '@/types'

// 用户分页查询参数
export interface UserPageParams extends PageParams {
  id?: number
  userType?: string
  nickName?: string
  phone?: string
  email?: string
  userStatus?: string
  schoolId?: number
  grade?: string
  createTime?: string
  isDeleted?: number
}

export class UserService {
  /**
   * 分页查询用户列表
   */
  static async getUserPage(params: UserPageParams): Promise<PageResult<User>> {
    const response = await api.get<PageResult<User>>('/auth/api-admin/authUser/page', params)
    return response.data
  }

  /**
   * 获取用户详情
   */
  static async getUserDetail(id: number): Promise<User> {
    const response = await api.get<User>(`/auth/api-admin/authUser/get/${id}`)
    return response.data
  }

  /**
   * 新增用户
   */
  static async addUser(userData: Partial<User>): Promise<boolean> {
    const response = await api.post<boolean>('/api-admin/authUser/add', userData)
    return response.data
  }

  /**
   * 更新用户
   */
  static async updateUser(userData: Partial<User>): Promise<boolean> {
    const response = await api.post<boolean>('/api-admin/authUser/update', userData)
    return response.data
  }

  /**
   * 删除用户
   */
  static async deleteUser(id: number): Promise<boolean> {
    const response = await api.post<boolean>('/auth/api-admin/authUser/delete', { id })
    return response.data
  }

  /**
   * 为用户分配角色
   */
  static async assignRoles(userId: number, roleIds: number[]): Promise<boolean> {
    const response = await api.post<boolean>('/auth/api-admin/authUser/assignRoles', {
      userId,
      roleIds
    })
    return response.data
  }

  /**
   * 重置用户密码
   */
  static async resetPassword(userId: number, newPassword: string): Promise<boolean> {
    const response = await api.post<boolean>('/api-admin/authUser/resetPassword', {
      userId,
      newPassword
    })
    return response.data
  }

  /**
   * 启用/禁用用户
   */
  static async toggleUserStatus(userId: number, status: string): Promise<boolean> {
    const response = await api.post<boolean>('/auth/api-admin/authUser/toggleStatus', {
      userId,
      status
    })
    return response.data
  }
}

// 默认导出单例实例
export const userService = UserService 